﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MF.BO.Objects;
using MF.DAL.EF;
using System.Data.Linq;
using System.Data.Entity;


namespace MF.DAL.Core
{
    public static partial class DALUtils
    {
        public static class ActivityDB
        {
            public static List<ActivityItem> GetAllActivity()
            {
                List<ActivityItem> toReturn = new List<ActivityItem>();

                try
                {
                    using (myFinanceEntities ctx = new myFinanceEntities())
                    {
                        toReturn = (from row in ctx.Activity
                                    select new ActivityItem
                                    {
                                        Id = row.Id,
                                        Description = row.Description,
                                        Amount = Convert.ToDecimal(row.Amount),
                                        InOut = Convert.ToBoolean(row.InOut),
                                        TransactionId = Convert.ToInt32(row.TransactionId),
                                        TDate = Convert.ToDateTime(row.TDate)
                                    }).ToList();
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }

                return toReturn;
            }

            public static ActivityItem GetActivityItemById(long ID)
            {
                ActivityItem toReturn = new ActivityItem();

                try
                {
                    using (myFinanceEntities ctx = new myFinanceEntities())
                    {
                        Activity dbObject = (from row in ctx.Activity where row.Id == ID select row).FirstOrDefault();

                        if (dbObject != null)
                        {
                            toReturn.Id = dbObject.Id;
                            toReturn.Description = dbObject.Description;
                            toReturn.Amount = dbObject.Amount;
                            toReturn.InOut = dbObject.InOut;
                            toReturn.TransactionId = dbObject.TransactionId;
                            toReturn.TDate = dbObject.TDate;
                        }
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }

                return toReturn;
            }

            public static int Insert(ActivityItem toInsert)
            {
                int toReturn = 0;

                try
                {
                    Activity dbObject = new Activity
                    {
                        Id = toInsert.Id,
                        Description = toInsert.Description,
                        Amount = toInsert.Amount,
                        InOut = toInsert.InOut,
                        TransactionId = toInsert.TransactionId,
                        TDate = toInsert.TDate
                    };

                    using (myFinanceEntities ctx = new myFinanceEntities())
                    {
                        ctx.AddToActivity(dbObject);
                        toReturn = ctx.SaveChanges();
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }

                return toReturn;
            }

            public static int Update(ActivityItem toUpdate)
            {
                int toReturn = 0;

                try
                {
                    using (myFinanceEntities ctx = new myFinanceEntities())
                    {
                        Activity dbObject = (from row in ctx.Activity where row.Id == toUpdate.Id select row).FirstOrDefault();

                        if (dbObject != null)
                        {
                            dbObject.Id = toUpdate.Id;
                            dbObject.Description = toUpdate.Description;
                            dbObject.Amount = toUpdate.Amount;
                            dbObject.InOut = toUpdate.InOut;
                            dbObject.TransactionId = toUpdate.TransactionId;
                            dbObject.TDate = toUpdate.TDate;
                        }
                       
                        toReturn = ctx.SaveChanges();
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }

                return toReturn;
            }

            public static int Delete(int toDelete)
            {
                int toReturn = 0;

                try
                {
                    using (myFinanceEntities ctx = new myFinanceEntities())
                    {
                        Activity dbObject = (from row in ctx.Activity where row.Id == toDelete select row).FirstOrDefault();

                        if (dbObject != null)
                        {
                            ctx.DeleteObject(dbObject);
                        }
                        
                        toReturn = ctx.SaveChanges();
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }

                return toReturn;
            }
        }
    }
}
